﻿using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;

namespace WPFExcelReport
{

    public class JLTSMPCOWSData : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public SETINTEXDB INTEXDB = new SETINTEXDB();
        public DataSet SetCAD;
        public DataSet SetExt
        {
            get
            {
                return Convert2Intex.SetExt;
            }
            set
            {
                Convert2Intex.SetExt = value;
            }
        }
        public Convert2Intex Convert2Intex;
        public JLTSMPCOWSData()
            : base()
        {
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
       
        public DataSet LoadVKPRICE(DataRow dwInput)
        {
           
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = INTEXDB.ICC_VKPRICE.NewRow();
                dwData["MANDANT"] = dwInput["MANDANT"];
                dwData["VKPART"] = dwInput["VKPART"];
                dwData["VKPAUFKRS"] = dwInput["VKPAUFKRS"];
                dwData["VKPAUFLFD"] = dwInput["VKPAUFLFD"];
                setResult = INTEXDBC.QueryICC_VKPRICE(dwData, 3);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet Reset(DataRow dwInput)
        {
            
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = null;
            dwData = INTEXDB.ICC_VKPRICE.NewRow();
           
            dwData["MANDANT"] = dwInput["MANDANT"];  
            dwData["VKPAUFKRS"] = dwInput["VKPAUFKRS"];
            dwData["VKPAUFLFD"] = dwInput["VKPAUFLFD"];  
            INTEXDBC.SaveICC_VKPRICE(dwData);
            INTEXDBC.DeleteICC_VKPWARP(dwData, 2);
            INTEXDBC.DeleteICC_VKPWEFT(dwData, 2);

            SResult = BuildData(dwInput, true);
            SReturn.Merge(SResult);
            return SReturn;
        }
        public DataSet BuildData(DataRow dwInput, bool IsAutoCreate = false)
        {
           
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = null;
            try
            {
                if (dwInput["CONSTRUCTION"].ToString().Trim() == "")
                {
                    dwData = INTEXDB.TXTIND.NewRow();
                    dwData["TXTIMANDANT"]= dwInput["MANDANT"];
                    dwData["TXTITXTART"]= "VAD";
                    dwData["TXTISIND"] = Left(dwInput["VKPKOLL"].ToString() + dwInput["VKPART"].ToString() + "                                                                                                          ", 60);
                    SResult = INTEXDBC.QueryTXTKON(dwData, 1);
                    string text = "";
                    foreach (DataRow dwTXT in SResult.Tables[0].Rows)
                    {
                        text += dwTXT["TXTKTEXT"].ToString();
                    }
                    dwInput["CONSTRUCTION"] = text;
                    INTEXDBC.SaveICC_VKPRICE(dwInput);                    
                }
                dwData = INTEXDB.VKPOS.NewRow();
                dwData["VKPMANDANT"] = dwInput["MANDANT"];
                dwData["VKPAUFKRS"] = dwInput["VKPAUFKRS"];
                dwData["VKPAUFLFD"] = dwInput["VKPAUFLFD"];
                SResult = INTEXDBC.QueryVKPOS(dwData, 2);
                SReturn.Merge(SResult);
                dwData = INTEXDB.ICC_VKPRICE.NewRow();
                dwData["MANDANT"] = dwInput["MANDANT"];
                dwData["ROWNID"] = dwInput["ROWNID"];
                dwData["VKPAUFKRS"] = dwInput["VKPAUFKRS"];
                dwData["VKPAUFLFD"] = dwInput["VKPAUFLFD"];
                SResult = INTEXDBC.QueryICC_VKPRICE(dwData, 1);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryICC_VKPWARP(dwData, 2); 
                if (SResult.Tables[0].Rows.Count == 0
                    && IsAutoCreate)
                {
                    Create(dwInput); 
                }
                SResult = INTEXDBC.QueryICC_VKPWARP(dwData, 2);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryICC_VKPWEFT(dwData, 2);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public void Create(DataRow dwInput)
        {
            DataRow dwData = null;

            try
            {
                #region========Head=======
                dwData = INTEXDB.ICC_VKPRICE.NewRow();
                dwData["CONTROL"] = "ON";
                dwData["MANDANT"] = dwInput["MANDANT"];
                dwData["ROWNID"] = dwInput["ROWNID"];
                dwData["MKUNDER"] = dwInput["MKUNDER"];
                dwData["KDKUNDNA"] = dwInput["KDKUNDNA"];
                dwData["VKPAUFKRS"] = dwInput["VKPAUFKRS"];
                dwData["VKPAUFLFD"] = dwInput["VKPAUFLFD"];
                dwData["VKPAUFPOS"] = dwInput["VKPAUFPOS"];
                dwData["VKPART"] = dwInput["VKPART"];
                dwData["VAABEZ"] = dwInput["VAABEZ"];
                dwData["CONSTRUCTION"] = dwInput["CONSTRUCTION"];
                dwData["VKPQTY"] = dwInput["VKPQTY"];
                dwData["VKPUNIT"] = dwInput["VKPUNIT"];
                dwData["USD2RMB"] = dwInput["USD2RMB"];
                dwData["CURRENCY"] = dwInput["CURRENCY"];
                dwData["MCPRICE"] = dwInput["MCPRICE"];
                dwData["PREH"] = dwInput["PREH"];
                dwData["MVKWEFTDEN"] = 0;
                dwData["MCOSTFADD"] = 0;
                dwData["MVKCOST"] = 0;
                dwData["MCOSTMW"] = 0;
                dwData["MCOSTMF"] = 0;
                dwData["MCOSTMY"] = 0;
                dwData["MCOSTDW"] = 0;
                dwData["MCOSTDF"] = 0;
                dwData["MCOSTDY"] = 0;
                dwData["MCOSTFIN"] = 0;
                dwData["MCOSTPAK"] = 0;
                dwData["MCOSTFVV"] = 0;
                dwData["MCOSTPROD"] = 0;
                dwData["MCOSTU"] = 0;
                dwData["MPRICEAGENT"] = 0;
                dwData["MCOSTAGENT"] = 0;
                dwData["MUNITAGENT"] = ConvertGB2312To8859P1("美分/米");
                dwData["MPRICESHIP"] = 0;
                dwData["MCOSTSHIP"] = 0;
                dwData["MUNITSHIP"] = ConvertGB2312To8859P1("美分/米");
                dwData["MPRICEFLIGHT"] = 0; ;
                dwData["MCOSTFLIGHT"] = 0;
                dwData["MUNITFLIGHT"] = ConvertGB2312To8859P1("元/米");
                dwData["MCOSTADD"] = 0;
                dwData["MUNITADD"] = 0;
                dwData["MCOSTGROSS"] = 0;
                dwData["MCOSTNET"] = 0;
                dwData["MCOSTNETW"] = 0;
                dwData["MCOSTF"] = 0;
                dwData["REMARK"] = dwInput["REMARK"];
                dwData["NEWUSER"] = dwInput["NEWUSER"];
                dwData["NEWTIME"] = dwInput["NEWTIME"];
                dwData["CHGUSER"] = UserID;
                dwData["CHGTIME"] = date;
                INTEXDBC.SaveICC_VKPRICE(dwData);
                #endregion
                #region=======Warp===========
                dwData = INTEXDB.ICC_VKPWARP.NewRow();
                dwData["CONTROL"] = "ON";
                dwData["MANDANT"] = dwInput["MANDANT"];
                dwData["ROWNID"] = "0";
                dwData["VKPAUFKRS"] = dwInput["VKPAUFKRS"];
                dwData["VKPAUFLFD"] = dwInput["VKPAUFLFD"];
                dwData["MPOS"] = "0010";
                dwData["MYARN"] = " ";
                dwData["MUNIT"] = "KG";
                dwData["MUSAGE"] = 0;
                dwData["MPRICEM"] = 0;
                dwData["MVALUEM"] = 0;
                dwData["MPRICED"] = 0;
                dwData["MVALUED"] = 0;
                dwData["MCOST"] = 0;
                dwData["NEWUSER"] = UserID;
                dwData["NEWTIME"] = date;
                dwData["CHGUSER"] = "";
                dwData["CHGTIME"] = "";
                INTEXDBC.SaveICC_VKPWARP(dwData);
                #endregion
                #region=======Weft===========
                dwData = INTEXDB.ICC_VKPWEFT.NewRow();
                dwData["CONTROL"] = "ON";
                dwData["MANDANT"] = dwInput["MANDANT"];
                dwData["ROWNID"] = "0";
                dwData["VKPAUFKRS"] = dwInput["VKPAUFKRS"];
                dwData["VKPAUFLFD"] = dwInput["VKPAUFLFD"];
                dwData["MPOS"] = "0010";
                dwData["MYARN"] = " ";
                dwData["MUNIT"] = "KG";
                dwData["MUSAGE"] = 0;
                dwData["MPRICEM"] = 0;
                dwData["MVALUEM"] = 0;
                dwData["MPRICED"] = 0;
                dwData["MVALUED"] = 0;
                dwData["MCOST"] = 0;
                dwData["NEWUSER"] = UserID;
                dwData["NEWTIME"] = date;
                dwData["CHGUSER"] = "";
                dwData["CHGTIME"] = "";
                INTEXDBC.SaveICC_VKPWEFT(dwData);
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public DataSet Refresh(DataRow dwInput)
        {
            
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = INTEXDB.ICC_VKPRICE.NewRow();
                dwData["MANDANT"] = dwInput["MANDANT"];
                INTEXDBC.InsertICC_VKPRICE(dwData, 2);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }

        public void SaveICC_VKPRICE(DataSet src, string client)
        {
            try
            {
                foreach (DataRow rw in src.Tables["ICC_GABBS"].Rows)
                {
                    INTEXDBC.SaveICC_VKPRICE(rw);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
